package com.itheima.wms.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.wms.common.enums.CheckStatus;
import com.itheima.wms.common.utils.RedisLock;
import com.itheima.wms.dao.AreaMapper;
import com.itheima.wms.dto.CheckDTO;
import com.itheima.wms.entity.AreaEntity;
import com.itheima.wms.entity.CheckEntity;
import com.itheima.wms.service.CheckService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration
public class CheckJob {

    /**
     * 计划盘点：为定时月盘点，和周盘点。月盘点，每个月20号21点，自动根据仓库生成每个仓库每个库区，按库位进行 的月盘点单。
     * <p>
     * 周盘点，每周五21点系统自动生成每个仓库，每个库区按库位的周盘点（取消）。
     */
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private CheckService checkServce;

    @Autowired
    private RedisLock redisLock;

    @Scheduled(cron = "0 0 21 20 * ?")     // 每个月20号21点
//    @Scheduled(cron = "0 3 17 * * ?")
    public void check() {
        log.info("定时盘点任务启动=================");
        String token = redisLock.tryLock("check-20-21", 1000 * 60);
        if (StringUtils.isNotBlank(token)) {
            List<Long> ids = new ArrayList<>();
            // 查询全部库区
            LambdaQueryWrapper<AreaEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.gt(AreaEntity::getIncludedNum, 0);
            List<AreaEntity> areaEntities = areaMapper.selectList(wrapper);
            log.info("全部库区：{}", areaEntities);
            // 遍历仓库生成盘点单
            areaEntities.forEach(areaEntity -> {
                try {
                    Long id = checkServce.generatorCheck(areaEntity);
                    ids.add(id);
                    log.info("库区：{} 已生成盘点单", areaEntity);
                } catch (Exception e) {
                    log.warn("库区：{} 生成盘点单异常", areaEntity, e);
                }
            });

            // 生成盘点任务
            checkServce.task(ids);
        }

        log.info("定时盘点任务结束=================");
    }


    @Scheduled(cron = "0 0/10 * * * ?") // 每十分钟一次
    public void checkExpire() {
        log.info("定时盘点任务 过期扫描 启动=================");
        LambdaQueryWrapper<CheckEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CheckEntity::getStatus, CheckStatus.NEW.getCode());
        List<CheckEntity> checks = checkServce.list(wrapper);

        checks.forEach(checkEntity -> {
            if (checkEntity.getPlanCheckTime().compareTo(LocalDateTime.now()) < 0) {
                log.info("任务：{} 已过期", checkEntity);
                CheckDTO checkDTO = new CheckDTO();
                checkDTO.setId(checkEntity.getId());
                checkServce.cancel(checkDTO);
            }
        });

        log.info("定时盘点任务 过期扫描 结束=================");
    }

}
